﻿using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class CnTrades : Information
    {
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("forward", "http://www.cntrades.com/");
            pd.Add("option", "username");
            pd.Add("username", name);
            pd.Add("password", MyFunction.Md5(pass));
            pd.Add("cookietime", "2592000");
            pd.Add("goto", "1");
            pd.Add("submit", " 登 录 ");
            string html = source.Post("http://www.cntrades.com/member/login.php", pd.Read(), "http://www.cntrades.com/member/login.php");
            if (html.IndexOf("<a href=\"http://www.cntrades.com/member/logout.php?forward=\">退出</a></span>") != -1)
            {
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            //取出空p标签
            content = Regex.Replace(content, "<p>\\s*</p>", "", RegexOptions.IgnoreCase);
            InformationResult result = new InformationResult();
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("action", "add");
            pd.Add("mid", "5");
            pd.Add("itemid", "0");
            pd.Add("forward", "http://www.cntrades.com/member/");
            pd.Add("post[typeid]", "0");
            pd.Add("post[title]", title);
            pd.Add("style", "");
            pd.Add("post[catid]", site.TypeId[0]);
            pd.Add("post[model]", pif.Get("产品名称"));
            pd.Add("post[standard]", pif.Get("产品描述"));
            pd.Add("post[brand]", pif.Get("产品名称"));
            pd.Add("post[content]", content);
            pd.Add("post[thumb]", UploadImage());
            pd.Add("post[thumb1]", UploadImage());
            pd.Add("post[thumb2]", UploadImage());
            pd.Add("post[totime]", "2014-02-16");
            pd.Add("post[unit]", "台");
            pd.Add("post[price]", pif.Get("产品价格"));
            pd.Add("post[minamount]", "1");
            pd.Add("post[amount]", "1");
            pd.Add("post[days]", "1");
            pd.Add("post[mycatid]", "0");
            pd.Add("post[elite]", "1");
            pd.Add("submit", " 提 交 ");
            string html = source.Post("http://www.cntrades.com/member/my.php", pd.Read(), "http://www.cntrades.com/member/login.php");
            if (Regex.IsMatch(html, "级别会员24小时内最多发布([0-9]+)条") || Regex.IsMatch(html, "cript\">alert\\('余额不足'\\)"))
            {
                result.Code = 3;
            }
            html = source.Get("http://www.cntrades.com/member/my.php?mid=5&status=2");
            if (Regex.IsMatch(html, "<a href=\"http://www.cntrades.com/sell/show-[0-9]+.html\" target=\"_blank\" class=\"t\">" + title + "</a>"))
            {
                result.Code = 1;
                result.Content = Regex.Match(html, "<a href=\"(http://www.cntrades.com/sell/show-[0-9]+.html)\" target=\"_blank\" class=\"t\">" + title + "</a>").Groups[1].Value;
            }
            return result;
        }
        //上传图片
        private string UploadImage()
        {
            string html = "";
            SourceFormData sfd = new SourceFormData("gb2312");
            //头部数据
            sfd.Write("fid", "1");
            sfd.Write("moduleid", "5");
            sfd.Write("from", "album");
            sfd.Write("old", "");
            sfd.Write("isremote", "0");
            sfd.Write("remote", "http://");
            //发送文件
            sfd.WriteFile("upalbum", RandomPicture.Get());
            //尾部数据
            sfd.Write("width", "100");
            sfd.Write("height", "100");
            //结尾
            html = source.Post("http://www.cntrades.com/upload.php", sfd.GetContentType(), sfd.Read(), "http://www.cntrades.com/member/my.php?mid=5&action=add");
            return Regex.Match(html, "getAlbum\\(\"(http://[^\"]+\\.jpg)\"").Groups[1].Value;
        }
    }
}
